package com.dayu.web.controller.recovery;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dayu.common.annotation.RepeatSubmit;
import com.dayu.recovery.domain.RecoveryGoodsRecord;
import com.dayu.recovery.domain.bo.GoodsQueryBo;
import com.dayu.recovery.domain.constant.GoodsConstant;
import com.dayu.recovery.service.ISalePriceLogService;
import com.dayu.security.web.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import com.dayu.common.annotation.Log;
import com.dayu.common.core.controller.BaseController;
import com.dayu.common.core.domain.AjaxResult;
import com.dayu.common.enums.BusinessType;
import com.dayu.recovery.domain.RecoveryGoods;
import com.dayu.recovery.service.IRecoveryGoodsService;
import com.dayu.common.utils.poi.ExcelUtil;
import com.dayu.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 回收商品管理Controller
 *
 * @author HK
 * @date 2021-04-16
 */
@RestController
@RequestMapping("/recovery/goods")
public class RecoveryGoodsController extends BaseController
{
    @Resource
    private IRecoveryGoodsService recoveryGoodsService;
    @Resource
    private PermissionService permissionService;

    @Resource
    private ISalePriceLogService salePriceLogService;
    /**
     * 查询回收商品管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(RecoveryGoods recoveryGoods)
    {
        startPage();
        List<RecoveryGoods> list = recoveryGoodsService.selectRecoveryGoodsList(recoveryGoods);
        return getDataTable(list);
    }

    @GetMapping("/record/list")
    public TableDataInfo recordList(RecoveryGoodsRecord bo) {
        startPage();
        List<RecoveryGoodsRecord> list = recoveryGoodsService.queryRecordList(bo);
        return getDataTable(list);
    }

    /**
     * 无作废数据
     * @param recoveryGoods
     * @return
     */
    @GetMapping("/void/list")
    public TableDataInfo toList(RecoveryGoods recoveryGoods) {
        Map<String, Object> map = new HashMap<>();
        map.put("noToVoid", "true");
        recoveryGoods.setParams(map);
        startPage();
        List<RecoveryGoods> list = recoveryGoodsService.selectRecoveryGoodsList(recoveryGoods);
        return getDataTable(list);
    }

    /**
     * 查询某个仓库 成本和
     */
    @PreAuthorize("@ss.hasPermi('recovery:goods:list')")
    @GetMapping("/sum/price")
    public AjaxResult priceSum(RecoveryGoods recoveryGoods) {
        if(recoveryGoods.getStockId() == null) {
            return AjaxResult.error("仓库编号不能为空！");
        }
        Map<String, Object> respMap = recoveryGoodsService.selectStockGoodsPrice(recoveryGoods);
        return AjaxResult.success(respMap);
    }

    /**
     * 导出回收商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('recovery:goods:export')")
    @Log(title = "回收商品管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RecoveryGoods recoveryGoods)
    {
        boolean bool = permissionService.hasPermi("goods:export:supplier");
        List<RecoveryGoods> list = recoveryGoodsService.exportRecoveryGoodsList(recoveryGoods, bool);
        ExcelUtil<RecoveryGoods> util = new ExcelUtil<RecoveryGoods>(RecoveryGoods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 获取回收商品管理详细信息
     */
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") String goodsId)
    {
        return AjaxResult.success(recoveryGoodsService.selectRecoveryGoodsById(goodsId));
    }

    /*正常商品详情*/
    @GetMapping("/normal/detail")
    public AjaxResult getInfoNormal(@RequestParam String goodsId) {
        return AjaxResult.success(recoveryGoodsService.queryGoodsNormalById(goodsId));
    }

    /**
     * 修改回收商品管理
     */
    @PreAuthorize("@ss.hasPermi('recovery:goods:edit')")
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@RequestBody RecoveryGoods recoveryGoods)
    {
        return toAjax(recoveryGoodsService.updateRecoveryGoods(recoveryGoods));
    }

    @PostMapping("/add/record")
    @RepeatSubmit
    public AjaxResult addGoodsRecord(@RequestBody RecoveryGoodsRecord record) {
        return AjaxResult.success(recoveryGoodsService.addGoodsRecord(record));
    }

    /**
     * 删除回收商品管理
     */
    @PreAuthorize("@ss.hasPermi('recovery:goods:remove')")
    @Log(title = "回收商品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable String[] goodsIds)
    {
        return toAjax(recoveryGoodsService.deleteRecoveryGoodsByIds(goodsIds));
    }

    /**
     * 验机修改
     */
    @PreAuthorize("@ss.hasPermi('recovery:goods:test')")
    @PutMapping("/test/update")
    @RepeatSubmit
    public AjaxResult updateByTest(@RequestBody RecoveryGoods recoveryGoods) {
        return toAjax(recoveryGoodsService.updateRecoveryGoods(recoveryGoods));
    }

    /*报价修改*/
    @PreAuthorize("@ss.hasPermi('recovery:goods:quotation')")
    @PutMapping("/quotation/update")
    @RepeatSubmit
    @Transactional
    public AjaxResult updateQuotation(@RequestBody RecoveryGoods goods) {
        if(!ObjectUtils.isEmpty(goods.getSalePriceList()) && !goods.getSalePriceList().isEmpty()) {
            goods.getSalePriceList().forEach(it -> {
                it.setGoodsId(goods.getGoodsId());
                salePriceLogService.insertSalePriceLog(it);
            });
        }
        return toAjax(recoveryGoodsService.updateRecoveryGoods(goods));
    }

    /**移交商品列表查询*/
    @GetMapping("/transfer/list")
    public AjaxResult transferList(RecoveryGoods goods) {
        goods.setGoodsStatus(GoodsConstant.GOODS_STATUS_OK);
        goods.setConfirmStatus(GoodsConstant.CONFIRM_STATUS_OK);
        startPage();
        List<RecoveryGoods> list = recoveryGoodsService.selectRecoveryGoodsList(goods);
        return AjaxResult.success(list);
    }

    //入库价格区间分布
    @GetMapping("/chatDataCostPrice")
    public AjaxResult chatDataCostPrice() {
        return AjaxResult.success(recoveryGoodsService.queryChatDataByCostPrice());
    }

    //成色分布
    @GetMapping("/chatDataFineness")
    public AjaxResult chatDataFineness() {
        return AjaxResult.success(recoveryGoodsService.queryFinenessChatData());
    }

    //品牌分布
    @GetMapping("/chatDataBrand")
    public AjaxResult chatDataBrand() {
        return AjaxResult.success(recoveryGoodsService.queryBrandChatData());
    }

    //最近7天销售和成本
    @GetMapping("/saleAndCost7")
    public AjaxResult saleAndCost7(GoodsQueryBo bo) {
        return AjaxResult.success(recoveryGoodsService.querySaleAndCostChatData(bo));
    }

    @GetMapping("/queryIndexData")
    public AjaxResult queryIndexData(GoodsQueryBo bo) {
        return AjaxResult.success(recoveryGoodsService.queryIndexData(bo));
    }
}
